package com.pb.wkflow.api.query;

import com.pb.infra.oth.util.SpringContextUtil;
import com.pb.wkflow.api.entity.TaskCountEntity;
import com.pb.wkflow.api.entity.TodoTaskEntity;
import com.pb.wkflow.api.mapper.TodoTaskMapper;
import com.pb.wkflow.core.entity.PostInfo;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.camunda.bpm.engine.impl.AbstractQuery;
import org.camunda.bpm.engine.impl.Page;
import org.camunda.bpm.engine.impl.interceptor.CommandContext;

import java.util.List;

/**
 * @author 苗益辉
 * @date 2022/5/18 15:19
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class TodoTaskQuery extends AbstractQuery<TodoTaskQuery, TodoTaskEntity> {

    private String taskId;
    private String activityId;
    private String activityName;
    private String description;
    private String executionId;
    private String processInstanceId;
    private String processDefineId;
    private String dueDate;
    private String suspensionState;
    private String tenantId;

    private String procDefineKey;
    private String procDefineKeyLike;
    private String procDefineName;
    private String procDefineNameLike;
    private String createTimeStart;
    private String createTimeEnd;
    private String createTimeSort;
    /**
     * 申请用户名称模糊查询
     */
    private String startUserNameLike;
    /**
     * 申请机构名称模糊查询
     */
    private String startOrgNameLike;
    /**
     * 任务开始时间开始
     */
    private String startTimeStart;
    /**
     * 任务开始时间结束
     */
    private String startTimeEnd;
    private List<PostInfo> postInfos;

    private final TodoTaskMapper actRuTaskMapper = SpringContextUtil.getBean(TodoTaskMapper.class);

    public void createTimeAsc() {
        this.createTimeSort = "asc";
    }

    public void createTimeDesc() {
        this.createTimeSort = "desc";
    }

    @Override
    public long executeCount(CommandContext commandContext) {
        return actRuTaskMapper.findTodoListCount(this);
    }

    @Override
    public List<TodoTaskEntity> executeList(CommandContext commandContext, Page page) {
        return actRuTaskMapper.findTodoList(this);
    }

    /**
     * 查询总数
     * @return long
     */
    public long getCount() {
        return actRuTaskMapper.findTodoListCount(this);
    }

    /**
     * 查询各流程类型待办数量
     * @return List<TaskCountEntity>
     */
    public List<TaskCountEntity> countProcDefKey() {
        return actRuTaskMapper.countProcDefKey(this);
    }
}
